/*
* SaMiR 3Dp - samir shaddad
* facebook:
* www.facebook.com/samir3dp
*
* instructables.com
* https://www.instructables.com/member/samir.3dp/
*
* youtube
* https://www.youtube.com/channel/UCY9JyPZwybxuTe4Wxq2ipUQ
*
* Instagram
* https://www.instagram.com/samir.3dp/
*
*/




//****** stepper ******
#include "CustomStepper.h"
//(Ardiono pins connected to UNL2003 )
//CustomStepper stepper(2, 3, 4, 5, (byte[]) { 8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 }, 4075.7728395, 12, CW);
//CustomStepper stepper2(9, 8, 7, 6, (byte[]) { 8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 }, 4075.7728395, 12, CW);
//*************************
CustomStepper stepper[2] = { CustomStepper(2, 3, 4, 5, (byte[]) { 8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 }, 4075.7728395, 2, CW),
CustomStepper(9, 8, 7, 6, (byte[]) { 8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 }, 4075.7728395, 4, CW) };



const int alphaSeg = 9;
const int betaSeg = 9;
const float alphaStep = 360.0 / alphaSeg;
const float betaStep = 180.0 / betaSeg;
const int nStep = alphaSeg*betaSeg;

int idx[nStep];
float degree[nStep];

void BuildMap()
{
	boolean forward = true;
	int i = 0;
	while (i < nStep - 1)
	{
		if (i%alphaSeg == alphaSeg - 1)
		{
			idx[i] = 0;
			degree[i] = betaStep;
			forward = !forward;
		}
		else
		{
			idx[i] = 1;
			if (forward)
			{
				degree[i] = alphaStep;
			}
			else
			{
				degree[i] = -alphaStep;
			}
		}
		i++;
	}

	idx[i] = 0;
	degree[i] = betaStep - 180.0;

}




void setup() {

	Serial.begin(9600);

	delay(4000);

	BuildMap();
}


void r(CustomStepper &sp, float dg)
{
	if (dg > 0)
	{
		sp.setDirection(CW);
		sp.rotateDegrees(dg);
	}
	else
	{
		sp.setDirection(CCW);
		sp.rotateDegrees(-dg);
	}
	while (!sp.isDone())	
		sp.run();	
}

void task(int stepperIndex[], float rotateDegree[], int n, unsigned long delayms)
{
	for (int i = 0; i < n; i++)
	{
		r(stepper[stepperIndex[i]], rotateDegree[i]);
		delay(delayms);
	}
}

int goo = 0;

void loop() 
{
	if (goo < 1)
	{
		task(idx, degree, nStep, 4000);

		//r(stepper[0], 360);

		goo++;
	}
}
